/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	heyong 
 * 
 * <p>描   述  :   BasicQualityController.java for com.pengtu.gsj.controller.checker.jsbz    </p>
 * 
 * <p>最后修改 : $: 2017年7月31日-下午5:22:52 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.controller.checker.jsbz.informatization;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.pengtu.gsj.controller.base.CheckTableBaseController;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.entity.check.CheckProject;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.DownService;
import com.pengtu.gsj.service.check.jsbz.informatization.BasicQualityService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

/**
 * 
 * BasicQualityController
 * 技术保障基础数据对应表内容控制器
 * 2017年7月31日 下午5:22:52
 * heyong
 * @version 1.0.0
 * 
 */
@Controller
@RequestMapping("basicQuality")
public class BasicQualityController extends CheckTableBaseController {
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	private BasicQualityService dasicQualityService;
	
	@Autowired
	private DownService downService;
	
	/**
	 * 跳转到技术保障基础数据表页面
	 * goToPage:
	 * 适用:
	 * @param model
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("show")
	@Token(save = true)
	public String goToPage(Model model) {
		String msg = SpringMvcUtils.getParameter("msg");
		if (StringUtils.isNotEmpty(msg)) {
			model.addAttribute("msg", msg);
		}
		return "check/jsbz/informatization/basicQuality";
	}
	
	
	/**
	 * 
	 * saveDevolp:
	 * 适用:保存基础数据表内容，包括搜索条件的存储
	 * @param checkTable
	 * @param checkProject
	 * @param attributes
	 * @return 绑定页面数据（left top projectId menuId） 重定向到检查表页面
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("save")
	@Token(remove = true)
	public String saveDevolp(@ModelAttribute CheckTable checkTable, @ModelAttribute CheckProject checkProject, RedirectAttributes attributes){
		checkTable.setCheckProject(checkProject);
		int years = systemManageService.getCheckYear(checkProject.getEndTime(), checkProject.getStartTime(), 1);
		checkTable.setTableFull(MathsUtils.formatDouble(checkTable.getYearFull()*years, 2)); //设置当前检查表格的总分
		checkTableService.save(checkTable);
		attributesParams(checkTable, attributes);
		return "redirect:/basicQuality/show.do";
	}
	
	/**
	 * 
	 * getBasicInfoMentMap:
	 * 适用:查询基础数据表内容，包括搜索条件的更新
	 * @param checkProject
	 * @return 
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value = "search", produces = "application/json; charset=utf-8")
	public Map<String, Object> getBasicInfoMentMap(@ModelAttribute CheckTable checkTable, @ModelAttribute CheckProject checkProject, @ModelAttribute Sysmenu sysmenu) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		//数据信息
		String tag = sysmenu == null ? "" : sysmenu.getTag();
		map =  dasicQualityService.getBasicInfoMent(checkProject.getStartTime(), checkProject.getEndTime(), checkTable, tag);
		
		return map;
	}
	
	
	/**
	 * 
	 * downWordAndZip:
	 * 适用:一键下载基础数据的表内容，附件，以及压缩打包
	 * @param checkProject
	 * @param checkTable
	 * @param sysmenu
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping("downWord")
	public ResponseEntity<byte[]> downWordAndZip(@ModelAttribute CheckProject checkProject, @ModelAttribute CheckTable checkTable, @ModelAttribute Sysmenu sysmenu) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String tag = sysmenu == null ? "" : sysmenu.getTag();
		String sysName = sysmenu == null ? "" : sysmenu.getName();
		//根据搜索条件查询出来的相关结果
		resultMap = dasicQualityService.getBasicInfoMent(checkProject.getStartTime(), checkProject.getEndTime(), checkTable, tag);
		return downService.downWordAndZip(SpringMvcUtils.getParameter("checkSubmoduleName"), SpringMvcUtils.getParameter("checkModuleName"), sysName, "technicalSupport/基础数据.xml", resultMap);
	}
}
